﻿namespace PlayListBundler
{
    using System.Collections.Generic;
    using System.IO;

    /// <summary>
    /// Coordinates the program copying and renaming processes. 
    /// Interface between GUI and logic.
    /// </summary>
    public class Manager
    {
        /// <summary>If true, new file names get generated track numbers.</summary>
        public bool WithTrackNumber { get; set; }
        /// <summary>The files (keys) and its modified filenames (values) in a 
        /// dictionary.</summary>        
        public Files Files { get; set; }
        /// <summary>The number of files.</summary>
        public int Count { get { return Files.Count; } }

        /// <summary>Initializes a new instance of the <see cref="Manager"/> class.
        /// </summary>
        public Manager()
        {
            Files = new Files();
        }

        private void BundleNewNames()
        {
            if (WithTrackNumber)
                for (int i = 0; i < Count; i++)
                {
                    string newName = Pattern.GetNameWithTrackNumber(i + 1, Files.NewName[i]);
                    Files.ChangeNewName(i, newName);
                }
            //else
            //{
            //    for (int i = 0; i < Count; i++)
            //    {
            //        string newName = Pattern.GetNameWithTrackNumber(i + 1, Files.NewName[i]);
            //        Files.ChangeNewName(i, newName);
            //    }            
            //}
        }

        /// <summary>Adds the passed files to the <see cref="Files"/> list.</summary>
        /// <param name="fileList">The files to add.</param>
        public void Add(string[] fileList)
        {
            foreach (string s in fileList)
                Files.Add(s);

            BundleNewNames();
        }

        /// <summary>
        /// Refills (sorts) the <see cref="Files"/> list by the passed 
        /// new <paramref name="fileList"/>. Note: Every file in the new filelist has to be
        /// already a file in the <see cref="Files"/> list.
        /// </summary>
        /// <param name="fileList">The new filelist.</param>
        public void ReFill(string[] fileList)
        {
            Files newFiles = new Files();
            for (int i = 0; i < fileList.Length; i++)
            {
                FFile file = Files.GetFFileFromOrigName(fileList[i]);
                newFiles.Add(file.Path);                
            }

            Files.Dispose();
            Files = newFiles;
            BundleNewNames();
        }

        /// <summary>
        /// Renames the file at the specified index of the list of all files.
        /// </summary>
        /// <param name="index">The index of the file to rename.</param>
        /// <param name="newName">The new name of the file to rename.</param>
        public void Rename(int index, string newName)
        { 
            Files.ChangeNewName(index, newName);
        }

        /// <summary>
        /// Renames the file (passed as full path) of the list of all files.
        /// </summary>
        /// <param name="path">The file (as full path) to rename.</param>
        /// <param name="newName">The new name of the file to rename.</param>
        public void Rename(string path, string newName)
        {
            Files.ChangeNewName(path, newName);
        }

        /// <summary>
        /// Renames all new names of all files by the passed <paramref name="newNames"/>.
        /// Note: The order in <see cref="Files"/> has to be matched with the order of 
        /// the passed <paramref name="newNames"/>.
        /// </summary>
        /// <param name="newNames">The new names.</param>
        public void Rename(string []newNames)
        {
            for (int i = 0; i < newNames.Length; i++)
                Rename(i, newNames[i]);
        }

        /// <summary>Removes the file of the list of all files.</summary>
        /// <param name="path">The file (as full path) to remove.</param>
        public void Remove(string path)
        {
            Files.Remove(path);
        }

        /// <summary>Removes the passed files to the <see cref="Files"/> list.</summary>
        /// <param name="fileList">The files to remove.</param>
        public void Remove(string[] fileList)
        {
            foreach (string s in fileList)
                Remove(s);
        }

        /// <summary>Removes all files from the <see cref="Files"/> list.</summary>
        public void RemoveAll()
        {
            Files.Clear();
        }

        /// <summary>
        /// Copies the files in the specified name format to the dictionary.
        /// </summary>
        /// <returns>True, if copy process was successful, otherwise false.</returns>
        public bool Copy(string path, ProgressBarForm form)
        {
            try
            {
                for (int i = 0; i < Files.Count; i++)
                {
                    form.Text = "Mp3 " + (i + 1).ToString() + " / " + Files.Count;
                    File.Copy(Files.Path[i], path + @"\" + Files.NewName[i], true);
                    form.Resultlabel.Text = @"Copy " + Files.NewName[i];
                    form.ProgressBar.PerformStep();
                    form.Refresh();
                }
                form.Dispose();
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
    }
}
